#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 3e5 + 10;
int st[N], ed[N];
vector<pair<int, long long> > fix[N];
vector<int> adj[N], cancel[N];
long long T[N << 2], lazy[N << 2];
int n, m, cnt;
void push(int s, int l, int r) {
if (!lazy[s]) return;
T[s] += lazy[s];
if (l != r) {
lazy[s << 1] += lazy[s];
lazy[s << 1 | 1] += lazy[s];
}
lazy[s] = 0;
}
void up(int s, int l, int r, int from, int to, long long val) {
push(s, l, r);
if (l > to || r < from) return;
if (from <= l && r <= to) {
lazy[s] += val;
push(s, l, r);
return;
}
int mid = l + r >> 1;
up(s << 1, l, mid, from, to, val);
up(s << 1 | 1, mid + 1, r, from, to, val);
T[s] = min(T[s << 1], T[s << 1 | 1]);
}
long long get(int s, int l, int r, int from, int to) {
push(s, l, r);
if (l > to || r < from) return 1e15;
if (from <= l && r <= to) return T[s];
int mid = l + r >> 1;
return min(get(s << 1, l, mid, from, to), get(s << 1 | 1, mid + 1, r, from, to));
}
void dfs(int u, int p = 0) {
st[u] = 1e9;
long long sum = 0;
for(int &v : adj[u]) if (v != p) {
dfs(v, u);
st[u] = min(st[u], st[v]);
sum += get(1, 1, m, st[v], ed[v]);
if (sum >= 1e18) {
cout << -1;
exit(0);
}
}
for(int &v : adj[u]) if (v != p) {
long long ex = get(1, 1, m, st[v], ed[v]);
up(1, 1, m, st[v], ed[v], sum - ex);
}
for(auto &[highest, cost] : fix[u]) {
++cnt; if (st[u] == 1e9) st[u] = cnt;
cancel[highest].push_back(cnt);
up(1, 1, m, cnt, cnt, cost + sum);
}
ed[u] = cnt;
if (u == 1) return;
for(auto &idx : cancel[u]) up(1, 1, m, idx, idx, 1e15);
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n >> m;
if (n == 1) return cout << 0, 0;
for(int i = 1; i < n; i++) {
int x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i = 1; i <= m; i++) {
int u, v, c; cin >> u >> v >> c;
fix[u].emplace_back(v, c);
}
dfs(1, 0);
long long ans = get(1, 1, m, st[1], ed[1]);
if (ans >= 1e15) ans = -1;
cout << ans;
}
20. Valid Parentheses | 746. Min Cost Climbing Stairs |
392. Is Subsequence | 70. Climbing Stairs |
53. Maximum Subarray | 1527A. And Then There Were K |
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers | 318. Maximum Product of Word Lengths |
448. Find All Numbers Disappeared in an Array | 1155. Number of Dice Rolls With Target Sum |
415. Add Strings | 22. Generate Parentheses |
13. Roman to Integer | 2. Add Two Numbers |
515. Find Largest Value in Each Tree Row | 345. Reverse Vowels of a String |
628. Maximum Product of Three Numbers | 1526A - Mean Inequality |
1526B - I Hate 1111 | 1881. Maximum Value after Insertion |
237. Delete Node in a Linked List | 27. Remove Element |
39. Combination Sum | 378. Kth Smallest Element in a Sorted Matrix |
162. Find Peak Element | 1529A - Eshag Loves Big Arrays |
19. Remove Nth Node From End of List | 925. Long Pressed Name |
1051. Height Checker | 695. Max Area of Island |